package cn.stylefeng.guns.modular.zdb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.zdb.entity.Zdb;
import cn.stylefeng.guns.modular.zdb.enums.ZdbExceptionEnum;
import cn.stylefeng.guns.modular.zdb.mapper.ZdbMapper;
import cn.stylefeng.guns.modular.zdb.pojo.request.ZdbRequest;
import cn.stylefeng.guns.modular.zdb.service.ZdbService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 站点表业务实现层
 *
 * @author zhu
 * @date 2021/10/06 11:28
 */
@Service
public class ZdbServiceImpl extends ServiceImpl<ZdbMapper, Zdb> implements ZdbService {

	@Override
    public void add(ZdbRequest zdbRequest) {
        Zdb zdb = new Zdb();
        BeanUtil.copyProperties(zdbRequest, zdb);
        this.save(zdb);
    }

    @Override
    public void del(ZdbRequest zdbRequest) {
        Zdb zdb = this.queryZdb(zdbRequest);
        this.removeById(zdb.getId());
    }

    @Override
    public void edit(ZdbRequest zdbRequest) {
        Zdb zdb = this.queryZdb(zdbRequest);
        BeanUtil.copyProperties(zdbRequest, zdb);
        this.updateById(zdb);
    }

    @Override
    public Zdb detail(ZdbRequest zdbRequest) {
        return this.queryZdb(zdbRequest);
    }

    @Override
    public PageResult<Zdb> findPage(ZdbRequest zdbRequest) {
        LambdaQueryWrapper<Zdb> wrapper = createWrapper(zdbRequest);
        Page<Zdb> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Zdb> findList(ZdbRequest zdbRequest) {
        LambdaQueryWrapper<Zdb> wrapper = this.createWrapper(zdbRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhu
     * @date 2021/10/06 11:28
     */
    private Zdb queryZdb(ZdbRequest zdbRequest) {
        Zdb zdb = this.getById(zdbRequest.getId());
        if (ObjectUtil.isEmpty(zdb)) {
            throw new BusinessException(ZdbExceptionEnum.ZDB_NOT_EXISTED);
        }
        return zdb;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhu
     * @date 2021/10/06 11:28
     */
    private LambdaQueryWrapper<Zdb> createWrapper(ZdbRequest zdbRequest) {
        LambdaQueryWrapper<Zdb> queryWrapper = new LambdaQueryWrapper<>();

        Long id = zdbRequest.getId();
        String zdbh = zdbRequest.getZdbh();
        String pybm = zdbRequest.getPybm();
        String zdmc = zdbRequest.getZdmc();
        String zdbz = zdbRequest.getZdbz();
        String zdzt = zdbRequest.getZdzt();
        String createTime = zdbRequest.getCreateTime();
        Long createUser = zdbRequest.getCreateUser();
        String updateTime = zdbRequest.getUpdateTime();
        Long updateUser = zdbRequest.getUpdateUser();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Zdb::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(zdbh), Zdb::getZdbh, zdbh);
        queryWrapper.like(ObjectUtil.isNotEmpty(pybm), Zdb::getPybm, pybm);
        queryWrapper.like(ObjectUtil.isNotEmpty(zdmc), Zdb::getZdmc, zdmc);
        queryWrapper.like(ObjectUtil.isNotEmpty(zdbz), Zdb::getZdbz, zdbz);
        queryWrapper.like(ObjectUtil.isNotEmpty(zdzt), Zdb::getZdzt, zdzt);
        queryWrapper.eq(ObjectUtil.isNotNull(createTime), Zdb::getCreateTime, createTime);
        queryWrapper.eq(ObjectUtil.isNotNull(createUser), Zdb::getCreateUser, createUser);
        queryWrapper.eq(ObjectUtil.isNotNull(updateTime), Zdb::getUpdateTime, updateTime);
        queryWrapper.eq(ObjectUtil.isNotNull(updateUser), Zdb::getUpdateUser, updateUser);

        return queryWrapper;
    }

}